package cn.mzcode.xyzpfuwuduan.service.impl;


import cn.mzcode.xyzpfuwuduan.common.Const;
import cn.mzcode.xyzpfuwuduan.common.ServerResponse;
import cn.mzcode.xyzpfuwuduan.dao.StudentMapper;
import cn.mzcode.xyzpfuwuduan.pojo.Student;
import cn.mzcode.xyzpfuwuduan.service.IStudentService;
import cn.mzcode.xyzpfuwuduan.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    //登录
    @Override
    public ServerResponse<Student> login(String username, String password) {
       int resultCount=studentMapper.checkUsername(username);
       if(resultCount==0){
           return ServerResponse.createByErrorMessage("用户名不存在");
       }
//       MD5加密登陆
       String md5Password=MD5Util.MD5EncodeUtf8(password);

       Student student=studentMapper.selectLogin(username,md5Password);
//        Student student=studentMapper.selectLogin(username,password);
       if (student==null){
           return ServerResponse.createByErrorMessage("密码错误");
       }
       student.setPassword(StringUtils.EMPTY);
       return  ServerResponse.createBySuccess("登录成功",student);
    }

    //注册
    @Override
    public ServerResponse<String> register(Student student) {
//        ServerResponse validResponse=this.checkVlaid(student.getUsername(),Const.USERNAME);
//            if (!validResponse.isSuccess()){
//                return validResponse;
//            }
//            validResponse=this.checkVlaid(student.getPassword(),Const.PHONE);
//            if (!validResponse.isSuccess()){
//                return validResponse;
//            }
//            validResponse=this.checkVlaid(student.getEmail(),Const.EMAIL);
//            if (!validResponse.isSuccess()){
//                return validResponse;
//            }
            student.setPassword(MD5Util.MD5EncodeUtf8(student.getPassword()));
            int resultCount=studentMapper.insert(student);
            if (resultCount==0){

                return ServerResponse.createByErrorMessage("注册失败");
            }
            return ServerResponse.createBySuccessMessage("注册成功");


    }

    //校验
    @Override
    public ServerResponse<String> checkVlaid(String str, String type) {
       if(StringUtils.isNoneBlank(type)){

           if (Const.USERNAME.equals(type)){
               int resultCount=studentMapper.checkUsername(str);
               if (resultCount>0){
                   return ServerResponse.createByErrorMessage("用户名已存在");
               }
           }
           if (Const.PHONE.equals(type)){
               int resltCount=studentMapper.checkPhone(str);
               if (resltCount>0){
                   return ServerResponse.createByErrorMessage("电话号码已被注册");
               }
           }
           if (Const.EMAIL.equals(type)){
               int resltCount=studentMapper.checkEmail(str);
               if (resltCount>0){
                   return ServerResponse.createByErrorMessage("邮箱已被注册");
               }
           }
       }else {
           return  ServerResponse.createByErrorMessage("参数错误");
       }
       return ServerResponse.createBySuccessMessage("校验成功");
    }

    //按手机号修改密码
    @Override
    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String phone) {
        if(phone.equals(null)) {
            return ServerResponse.createByErrorMessage("请填写验证手机号码");
        }
        ServerResponse validResponse=this.checkVlaid(username,Const.USERNAME);
        if(validResponse.isSuccess()){
            return ServerResponse.createByErrorMessage("用户不存在");
        }
        String sqlphone=studentMapper.selectPhoneByUsername(username);
        if (phone.equals(sqlphone)){
            String md5Password=MD5Util.MD5EncodeUtf8(passwordNew);
            int rowCount=studentMapper.updateByUsername(username,md5Password);
            if (rowCount>0){
                return ServerResponse.createBySuccessMessage("修改密码成功");
            }
            return ServerResponse.createByErrorMessage("验证手机号码错误");
            }
         return ServerResponse.createByErrorMessage("修改密码失败");
    }

    //登录状态下修改密码
    @Override
    public ServerResponse<String> resetPassword(String passwordOld, String passwordNew, Student student) {
        int resoultCount=studentMapper.checkPassword(MD5Util.MD5EncodeUtf8(passwordOld),student.getId());
        if (resoultCount==0){
            return ServerResponse.createByErrorMessage("旧密码错误");
        }
        student.setPassword(MD5Util.MD5EncodeUtf8(passwordNew));
        int updateCount=studentMapper.updateByPrimaryKeySelective(student);
        if(updateCount>0){
            return ServerResponse.createBySuccessMessage("密码更新成功");
        }
        return ServerResponse.createByErrorMessage("密码更新失败");
    }

    //更新用户信息
    @Override
    public ServerResponse<Student> updateInformation(Student student) {
        //username不能被更新
        //email和phone进行验证
        int resultCount=studentMapper.checkEmailByUserId(student.getEmail(),student.getId());
        if (resultCount>0){
            return ServerResponse.createByErrorMessage("邮箱已被占用，请更换email再尝试更新");
        }
        resultCount=studentMapper.checkPhoneByUserId(student.getPhone(),student.getId());
        if (resultCount>0){
            return ServerResponse.createByErrorMessage("手机号码已被占用，请更换号码再尝试更新");
        }
        Student updateStudent=new Student();
        updateStudent.setId(student.getId());
        updateStudent.setEmail(student.getEmail());
        updateStudent.setPhone(student.getPhone());
        updateStudent.setImage(student.getImage());
        updateStudent.setNickname(student.getNickname());

        int updateCount=studentMapper.updateByPrimaryKeySelective(updateStudent);
        if(updateCount>0){
            return ServerResponse.createBySuccess("个人信息更新成功",updateStudent);
        }
        return ServerResponse.createByErrorMessage("个人信息更新失败");
    }

    //获取当前用户信息
    public ServerResponse<Student> getInformation(Integer studentId){
        Student student=studentMapper.selectByPrimaryKey(studentId);
        if(student==null){
            return ServerResponse.createByErrorMessage("找不到当前用户");
        }
        student.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess(student);
    }

}
